﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Whistler
{
    public partial class Whistler : Form
    {
        private int _minimumTimeInSeconds;
        private int _maximumTimeInSeconds;
        private bool _running;

        private readonly System.Timers.Timer timer = new System.Timers.Timer();
        private readonly Random rand = new Random();

        private readonly List<string> _wavFiles = new List<string>();

        public Whistler()
        {
            InitializeComponent();
            UpdateButtonState();

            timer.AutoReset = false;
            timer.Elapsed += timer_Elapsed;

            _minimumTimeInSeconds = (int)minimumTimeTextBox.Value;
            _maximumTimeInSeconds = (int)maximumTimeTextBox.Value;
        }

        private void startStopButton_Click(object sender, EventArgs e)
        {
            if (_running)
            {
                //Stop the timer
                _running = false;
                StopTimer();
            }
            else
            {
                if (_wavFiles.Count > 0)
                {
                    //Start the timer
                    _running = true;
                    StartTimer();
                }
                else
                {
                    MessageBox.Show(this, "You need to add at least one sound to the list of WAV files", "No Sounds To Play", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            UpdateButtonState();
        }

        private delegate void NoArgumentDelegate();

        private void StartTimer()
        {
            int timespan = _maximumTimeInSeconds - _minimumTimeInSeconds;
            int wait = _minimumTimeInSeconds + rand.Next(timespan + 1);
            wait *= 1000;
            timer.Interval = wait;
            timer.Start();
        }

        private void StopTimer()
        {
            timer.Stop();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int randomIndex = rand.Next(_wavFiles.Count);
            
            //We're playing this synchronously because we're on a background thread and we don't
            //want to start the next timer until the sound has finished playing
            Sound.PlaySound(_wavFiles[randomIndex], false); 

            //Need to beginInvoke because this will come back on a non-UI thread
            BeginInvoke(new NoArgumentDelegate(StartTimer));
        }

        private void minimumTimeTextBox_ValueChanged(object sender, EventArgs e)
        {
            _minimumTimeInSeconds = (int)minimumTimeTextBox.Value;

            if (_minimumTimeInSeconds > _maximumTimeInSeconds)
            {
                maximumTimeTextBox.Value = _minimumTimeInSeconds;
            }
        }

        private void maximumTimeTextBox_ValueChanged(object sender, EventArgs e)
        {
            _maximumTimeInSeconds = (int)maximumTimeTextBox.Value;

            if (_maximumTimeInSeconds < _minimumTimeInSeconds)
            {
                minimumTimeTextBox.Value = _maximumTimeInSeconds;
            }
        }

        private void addFileButton_Click(object sender, EventArgs e)
        {
            DialogResult result = openWavFileDialog.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                _wavFiles.AddRange(openWavFileDialog.FileNames);
                RefreshWavFileList();
            }
        }

        private void removeFileButton_Click(object sender, EventArgs e)
        {
            foreach (int i in wavFileListView.SelectedIndices)
            {
                _wavFiles.RemoveAt(i);
            }
        }

        private void RefreshWavFileList()
        {
            wavFileListView.Items.Clear();
            for(int i = 0; i < _wavFiles.Count; i++)
            {
                string[] values = new string[] { i.ToString(), _wavFiles[i] };
                ListViewItem newItem = new ListViewItem(values);
                wavFileListView.Items.Add(newItem);
            }
        }

        private void UpdateButtonState()
        {
            if (_running)
            {
                startStopButton.Text = "Stop";
                stateLabel.Text = "Running...";
                stateLabel.BackColor = Color.LightGreen;
            }
            else
            {
                startStopButton.Text = "Start";
                stateLabel.Text = "Stopped";
                stateLabel.BackColor = Color.LightPink;
            }
        }
    }
}
